home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1994 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.1 KB  |  67 lines

  1. Path: ix.netcom.com!netnews
  2. From: mcorcora@ix.netcom.com(Marian Corcoran )
  3. Newsgroups: comp.lang.c++
  4. Subject: RE: Function body banned in .H file - Can I still inline?
  5. Date: 14 Jan 1996 22:42:11 GMT
  6. Organization: Netcom
  7. Message-ID: <4dc0s3$dlo@ixnews7.ix.netcom.com>
  8. References: <4d0ir0$68e@newsroom.hitc.com> <00001a81+00008dc1@msn.com>
  9. NNTP-Posting-Host: ix-sj35-03.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 14  2:42:11 PM PST 1996
  11.  
  12. In <00001a81+00008dc1@msn.com> SimsGW@msn.com (Gary Sims) writes: 
  13. >
  14. >I think you've misunderstood something you read, Chris. You can put 
  15. >the body of a function in the header file. That's the only practical 
  16. >way to have inline functions. What you can NOT do is present the body 
  17. >to the compiler more than once. Since the same header is frequently 
  18. >#include'd by one more than one module, you must prevent it being 
  19. >interpreted twice. Here's how you do that. I've included an example 
  20. >of the two ways to specify an inline function:
  21. >
  22. >#ifndef THIS_HEADER_NAME
  23. >#define THIS_HEADER_NAME
  24. >
  25. >class Foo
  26. >{
  27. >   int PrivateXValue;
  28. >public:
  29. >   Foo();
  30. >   int GetX(){return PrivateXValue;};
  31. >   int GetFractionalX(const int Divisor);
  32. >};
  33. >
  34. >int inline Foo::GetFractionalX(const int Divisor)
  35. >{
  36. >   return PrivateXValue/Divisor;
  37. >};
  38. >
  39. >#endif
  40. >
  41. >The first time the compiler sees this, the value THIS_HEADER_NAME 
  42. >will not have been defined, so the lines between there and the #endif 
  43. >will be processed. On successive occasions, it WILL be defined, so 
  44. >they won't. That avoids the problem of double definitions of the 
  45. >function body. Hope this helps,
  46. >
  47. >Gary W. Sims
  48. >Stonehaven Laboratory
  49.  
  50. There is also another technique to speed things up.  Say the name
  51. of the file is header1.h.  You can use a conditional in the 
  52. source where you are going to do the #include
  53.  
  54. #ifndef __HEADER1_H__
  55. #include "header1.h"
  56. #endif
  57.  
  58. That will save you the time of going out to see if the header
  59. has been included.  There is some disagreement about whether
  60. this is a good idea.  One guide says it is error-prone another
  61. guide recommends it.  Of course, you would still put the 
  62. #ifndef ... as originally suggested above in the header file itself.
  63.  
  64.  
  65. Marian
  66.   
  67.